﻿using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
using PepsiCo.Common;
using T2V.SharePoint.Common.Utilities;

namespace PepsiCo.RBS.EventReceivers.MeetingRoomEventReceiver
{
    /// <summary>
    /// List Item Events
    /// </summary>
    public class MeetingRoomEventReceiver : SPItemEventReceiver
    {
        /// <summary>
        /// An item is being added.
        /// </summary>
        public override void ItemAdding(SPItemEventProperties properties)
        {
            base.ItemAdding(properties);
            UpdateResourcePrivateForAdding(properties);
        }

        /// <summary>
        /// An item is being updated.
        /// </summary>
        public override void ItemUpdating(SPItemEventProperties properties)
        {
            base.ItemUpdating(properties);
            UpdateResourcePrivateForUpdating(properties);
        }

        /// <summary>
        /// An item is being deleted.
        /// </summary>
        public override void ItemDeleting(SPItemEventProperties properties)
        {
            base.ItemDeleting(properties);
            UpdateResourcePrivateForDeleting(properties);
        }

        public void UpdateResourcePrivateForAdding(SPItemEventProperties properties)
        {
            if (properties.AfterProperties[PepsiCoConstants.MeetingRoom.AVAILABLE_RESOURCES_COLUMN] != null && 
                !string.IsNullOrEmpty(properties.AfterProperties[PepsiCoConstants.MeetingRoom.AVAILABLE_RESOURCES_COLUMN].ToString()))
            {
                SPFieldLookup lookup = (SPFieldLookup)properties.List.Fields[PepsiCoColumnId.MeetingRoom.AvailableResources];
                SPList resourceLookupList = properties.Web.Lists[new Guid(lookup.LookupList)];

                SPFieldLookupValueCollection resourcesLookupAfter = new SPFieldLookupValueCollection(properties.AfterProperties[PepsiCoConstants.MeetingRoom.AVAILABLE_RESOURCES_COLUMN].ToString());

                using (DisableItemEvent scope = new DisableItemEvent())
                {
                    foreach (var resourceLookupAfter in resourcesLookupAfter)
                    {
                        SPListItem resourceItem = resourceLookupList.GetItemById(resourceLookupAfter.LookupId);
                        resourceItem[PepsiCoColumnId.Resource.IsPrivate] = true;
                        resourceItem.SystemUpdate(false);
                    }
                }
            }
        }

        public void UpdateResourcePrivateForUpdating(SPItemEventProperties properties)
        {
            var AR_Before = properties.ListItem[PepsiCoColumnId.MeetingRoom.AvailableResources];
            var AR_After = properties.AfterProperties[PepsiCoConstants.MeetingRoom.AVAILABLE_RESOURCES_COLUMN];

            SPFieldLookup lookup = (SPFieldLookup)properties.List.Fields[PepsiCoColumnId.MeetingRoom.AvailableResources];
            SPList resourceLookupList = properties.Web.Lists[new Guid(lookup.LookupList)];

            SPFieldLookupValueCollection resourcesLookupBefore = new SPFieldLookupValueCollection(properties.ListItem[PepsiCoColumnId.MeetingRoom.AvailableResources].ToString());
            SPFieldLookupValueCollection resourcesLookupAfter = new SPFieldLookupValueCollection(properties.AfterProperties[PepsiCoConstants.MeetingRoom.AVAILABLE_RESOURCES_COLUMN].ToString());

            using (DisableItemEvent scope = new DisableItemEvent())
            {
                //is New then set Private = true
                foreach (var resourceLookupAfter in resourcesLookupAfter)
                {
                    bool isNew = true;

                    foreach (var resourceLookupBefore in resourcesLookupBefore)
                    {
                        if (resourceLookupAfter.LookupId == resourceLookupBefore.LookupId)
                        {
                            isNew = false;
                            break;
                        }
                    }

                    if (isNew)
                    {
                        SPListItem resourceItem = resourceLookupList.GetItemById(resourceLookupAfter.LookupId);
                        resourceItem[PepsiCoColumnId.Resource.IsPrivate] = true;
                        resourceItem.SystemUpdate(false);
                    }
                }

                //is Deleted then set Private = false
                foreach (var resourceLookupBefore in resourcesLookupBefore) 
                {
                    bool isDeleted = true;

                    foreach (var resourceLookupAfter in resourcesLookupAfter)
                    {
                        if (resourceLookupAfter.LookupId == resourceLookupBefore.LookupId)
                        {
                            isDeleted = false;
                            break;
                        }
                    }

                    if (isDeleted)
                    {
                        SPListItem resourceItem = resourceLookupList.GetItemById(resourceLookupBefore.LookupId);
                        resourceItem[PepsiCoColumnId.Resource.IsPrivate] = false;
                        resourceItem.SystemUpdate(false);
                    }
                }
            }
        }

        public void UpdateResourcePrivateForDeleting(SPItemEventProperties properties)
        {
            if (properties.ListItem[PepsiCoColumnId.MeetingRoom.AvailableResources] != null && 
                !string.IsNullOrEmpty(properties.ListItem[PepsiCoColumnId.MeetingRoom.AvailableResources].ToString()))
            {
                SPFieldLookup lookup = (SPFieldLookup)properties.List.Fields[PepsiCoColumnId.MeetingRoom.AvailableResources];
                SPList resourceLookupList = properties.Web.Lists[new Guid(lookup.LookupList)];

                SPFieldLookupValueCollection resourcesLookupBefore = new SPFieldLookupValueCollection(properties.ListItem[PepsiCoColumnId.MeetingRoom.AvailableResources].ToString());

                using (DisableItemEvent scope = new DisableItemEvent())
                {
                    foreach (var resourceLookupBefore in resourcesLookupBefore)
                    {
                        SPListItem resourceItem = resourceLookupList.GetItemById(resourceLookupBefore.LookupId);
                        resourceItem[PepsiCoColumnId.Resource.IsPrivate] = false;
                        resourceItem.SystemUpdate(false);
                    }
                }
            }
        }
    }
}
